from flask import Flask, render_template
import pymysql

app = Flask(__name__, template_folder='.')  # 这里通过 '.' 指定当前目录为模板文件夹

@app.route('/')
def index():
    # 连接MySQL数据库
    connection = pymysql.connect(
        host='localhost',
        user='root',
        password='mysqlwxq0805',
        database='real_time_student_info',
        charset='utf8mb4'
    )
    try:
        with connection.cursor() as cursor:
            # 查询各个班级男女的人数数据的SQL语句
            sql = "SELECT classId, maleCount, femaleCount FROM class_gender_count"
            cursor.execute(sql)
            results = cursor.fetchall()
            data = []
            for row in results:
                class_id = row[0]
                male_count = row[1]
                female_count = row[2]
                data.append({
                    "classId": class_id,
                    "maleCount": male_count,
                    "femaleCount": female_count
                })
            return render_template('index5.html', data=data)
    finally:
        connection.close()

if __name__ == '__main__':
    app.run(debug=True)